package com.nxy.cpfsfe.service.factory;

import com.adtec.starring.exception.BaseException;
import com.adtec.starring.global.SysDef;
import com.adtec.starring.respool.SystemParameter;
import com.nxy.cpfsfe.enumtype.MsgCdOrTpEnum;
import com.nxy.cpfsfe.enumtype.RespCd;
import com.nxy.cpfsfe.util.*;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Map;

/**
 * 信用卡信息校验查询处理
 * Created by wyj on 2019/12/27.
 */
public class CrCrdInfQryServer implements ForwardServer{
    private static CrCrdInfQryServer server = new CrCrdInfQryServer();
    private CrCrdInfQryServer(){}
    public static CrCrdInfQryServer getInstance(){
        return server;
    }
    private final static Logger LOGGER = LoggerFactory.getLogger(CrCrdInfQryServer.class);

    @Override
    public String buildEsbXml(Map<String, String> map) {
        LOGGER.info("---------信用卡信息校验查询报文CBS0010800--------");
        try {
            XMLNodeUtil xmlReqDoc = new XMLNodeUtil(XMLNodeUtil.FROM_FILE,
                    SysDef.WORK_DIR + SystemParameter.SRC_PATH + SysDef.FILE_SP + "xml" + SysDef.FILE_SP + "CBS" + SysDef.FILE_SP + "CBS_0010800.xml");
            //报文头
            XmlCBSBuildUtil.buildXmlRequestHead(xmlReqDoc, MsgCdOrTpEnum.CBS_0010800.getCode(), map);
            //报文体
            xmlReqDoc.setNodeContentTrim("/transaction/body/request/cardNb ",StringUtil.trimObject(map.get("/root/MsgBody/AcctInf/AcctNo")));
            xmlReqDoc.setNodeContentTrim("/transaction/body/request/cvv ",StringUtil.trimObject(map.get("/root/MsgBody/AcctInf/CVV")));
            xmlReqDoc.setNodeContentTrim("/transaction/body/request/validDate ",StringUtil.trimObject(map.get("/root/MsgBody/AcctInf/ValidDt")));
            xmlReqDoc.setNodeContentTrim("/transaction/body/request/transPwd ",StringUtil.trimObject(map.get("/root/MsgBody/AcctInf/AcctransPwd")));
            xmlReqDoc.setNodeContentTrim("/transaction/body/request/memo ",StringUtil.trimObject(map.get("/root/MsgBody/AcctInf/TrxRemark")));
            return xmlReqDoc.toString();
        } catch (Exception e) {
            LOGGER.error("信用卡信息校验查询报文CBS0010800失败", e);
            throw new BaseException(RespCd.NCPS2402, e, "信用卡信息校验查询报文CBS0010800失败");
        }
    }

    @Override
    public String handleRpsMsg(Map<String, String> map, String respMsg) {
        LOGGER.info("---------信用卡信息校验查询回执报文CBS0010801--------");
        try {
            XMLNodeUtil xmlReqDoc = new XMLNodeUtil(XMLNodeUtil.FROM_FILE,
                    SysDef.WORK_DIR + SystemParameter.SRC_PATH + SysDef.FILE_SP + "xml" + SysDef.FILE_SP + "CPFS" + SysDef.FILE_SP + "CPFS_309.xml");
            //报文头
            XmlCPFSBuildUtil.buildXmlResponseHead(xmlReqDoc, MsgCdOrTpEnum.MSGTD_309.getCode(), map);
            //解析cbs0010801报文
            if(StringUtils.isNotBlank(respMsg)){
                Map<String,String> rpsmap = XmlUtil.xml2Map(respMsg);
                String  retCd = StringUtil.trimObject(rpsmap.get("/transaction/header/status/retCd"));
                //返回码前两位为00代表成功
                if(retCd.startsWith("00")&&retCd.length() ==6){
                    map.put("msg", StringUtil.trimObject(rpsmap.get("/transaction/header/status/desc")));
                    map.put("resultCode",retCd);
                    map.put("custID",StringUtil.trimObject(rpsmap.get("/transaction/body/response/custID")));
                    map.put("custNm",StringUtil.trimObject(rpsmap.get("/transaction/body/response/custNm")));
                    map.put("openBrchID",StringUtil.trimObject(rpsmap.get("/transaction/body/response/openBrchID")));
                    map.put("openBrchNm",StringUtil.trimObject(rpsmap.get("/transaction/body/response/openBrchNm")));
                    map.put("openBrchNmSt",StringUtil.trimObject(rpsmap.get("/transaction/body/response/openBrchNmSt")));
                    map.put("stat",StringUtil.trimObject(rpsmap.get("/transaction/body/response/stat")));
                    map.put("checkResult",StringUtil.trimObject(rpsmap.get("/transaction/body/response/checkResult")));
                    map.put("errRes",StringUtil.trimObject(rpsmap.get("/transaction/body/response/errRes")));
                    map.put("certType",StringUtil.trimObject(rpsmap.get("/transaction/body/response/certType")));
                    map.put("certNo",StringUtil.trimObject(rpsmap.get("/transaction/body/response/certNo")));
                    map.put("phoneNo",StringUtil.trimObject(rpsmap.get("/transaction/body/response/phoneNo")));
                    map.put("errCode",StringUtil.trimObject(rpsmap.get("/transaction/body/response/errCode")));
                    map.put("errMsg",StringUtil.trimObject(rpsmap.get("/transaction/body/response/errMsg")));
                    map.put("transDesc",StringUtil.trimObject(rpsmap.get("/transaction/body/response/transDesc")));
                }else{
                    map.put("msg", StringUtil.trimObject(rpsmap.get("/transaction/header/status/desc")));
                    map.put("resultCode",retCd);
                }
            }else{
                LOGGER.error("cbs0010801回执报文为空，请联系");
                map.put("msg", "cbs0010801回执报文为空，请联系");
                map.put("resultCode",RespCd.NCPO0005);
            }
            //报文体
            xmlReqDoc.setNodeContentTrim("/root/MsgBody/BizInf/BizStsCd",  StringUtil.trimObject(map.get("resultCode")));
            xmlReqDoc.setNodeContentTrim("/root/MsgBody/BizInf/BizStsDesc",  StringUtil.trimObject(map.get("msg")));
            xmlReqDoc.setNodeContentTrim("/root/MsgBody/AcctInf/AcctNo",  StringUtil.trimObject(map.get("certNo")));
            xmlReqDoc.setNodeContentTrim("/root/MsgBody/AcctInf/AcctNm",  StringUtil.trimObject(map.get("custNm")));
            xmlReqDoc.setNodeContentTrim("/root/MsgBody/AcctInf/BrnchId",  StringUtil.trimObject(map.get("openBrchID")));
            xmlReqDoc.setNodeContentTrim("/root/MsgBody/AcctInf/BrnchNm",  StringUtil.trimObject(map.get("openBrchNm")));
            xmlReqDoc.setNodeContentTrim("/root/MsgBody/AcctInf/BrchNmSt",  StringUtil.trimObject(map.get("openBrchNmSt")));
            xmlReqDoc.setNodeContentTrim("/root/MsgBody/AcctInf/AcctSts",  StringUtil.trimObject(map.get("stat")));
            xmlReqDoc.setNodeContentTrim("/root/MsgBody/AcctInf/RetCode",  StringUtil.trimObject(map.get("checkResult")));
            xmlReqDoc.setNodeContentTrim("/root/MsgBody/AcctInf/ErrRes",  StringUtil.trimObject(map.get("errRes")));
            xmlReqDoc.setNodeContentTrim("/root/MsgBody/AcctInf/CertTp",  StringUtil.trimObject(map.get("certType")));
            xmlReqDoc.setNodeContentTrim("/root/MsgBody/AcctInf/CertNo",  StringUtil.trimObject(map.get("certNo")));
            xmlReqDoc.setNodeContentTrim("/root/MsgBody/AcctInf/AcctMobileNo",  StringUtil.trimObject(map.get("phoneNo")));
            xmlReqDoc.setNodeContentTrim("/root/MsgBody/AcctInf/TransDesc",  StringUtil.trimObject(map.get("transDesc")));

            return xmlReqDoc.toString();
        } catch (Exception e) {
            LOGGER.error("信用卡信息校验查询回执报文CBS0010801失败", e);
            throw new BaseException(RespCd.NCPS2402, e, "信用卡信息校验查询回执报文CBS0010801失败");
        }
    }
}